﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"snake.h"

void SetPos(short x, short y)
{
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = { x, y };

	//设置标准输出上光标的位置为pos
	SetConsoleCursorPosition(handle, pos);
}

void WelcomeToGame()
{
	//欢迎信息
	SetPos(35, 12);
	printf("欢迎来到贪吃蛇小游戏！");
	SetPos(38, 25);
	system("pause");
	system("cls");

	//功能介绍信息
	SetPos(25, 12);
	printf("用↑ ↓ ← →来控制蛇的移动，F3是加速，F4是减速\n");
	SetPos(25, 13);
	printf("加速能得到更高的分数！");
	SetPos(38, 25);
	system("pause");
	system("cls");
}

void CreateMap()
{
	//上
	SetPos(0, 0);
	//一个宽字符占用两个字节
	for (int i = 0; i <= 56; i += 2)
	{
		wprintf(L"%lc", WALL);
	}
	//下
	SetPos(0, 26);
	for (int i = 0; i < 56; i += 2)
	{
		wprintf(L"%lc", WALL);
	}
	//左
	for (int i = 1; i <= 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	//右
	for (int i = 1; i <= 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}

void InitSnake(pSnake ps)
{
	//创建五个蛇身的节点
	pSnakeNode pcur = NULL;
	for (int i = 0; i < 5; i++)
	{
		pcur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (pcur == NULL)
		{
			perror("InitSnake::malloc");
			return;
		}
		//初始蛇身的位置
		pcur->x = POS_X + 2 * i;
		pcur->y = POS_Y;
		pcur->next = NULL;

		//头插法
		//首先创建蛇尾
		if (ps->pSnake == NULL)
		{
			ps->pSnake = pcur;
		}
		//再接着从蛇尾巴往蛇头链接
		else
		{
			pcur->next = ps->pSnake;  //pcur指向下一个节点
			ps->pSnake = pcur;  //pSnake指向下一个节点
		}
	}

	//打印蛇身
	pcur = ps->pSnake;  //先让pcur指向蛇头
	while (pcur)
	{
		SetPos(pcur->x, pcur->y);
		wprintf(L"%lc", BODY);
		pcur = pcur->next;
	}
	//贪吃蛇其他信息的初始化
	ps->direction = RIGHT;
	ps->FoodWeight = 10;
	ps->pFood = NULL;
	ps->Score = 0;
	ps->Speed = 200;
	ps->status = OK;
}

void CreateFood(pSnake ps)
{
	//随机出现；不能在蛇身上；必须在墙内；横坐标必须是2的倍数（因为是宽字符，每个占两个字节）
	int x = 0;
	int y = 0;
again:
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 24 + 1;
	} while (x % 2 != 0);  //食物的横坐标不能为奇数，因为蛇的横坐标不是奇数，不然影响游戏效果，且这是宽字符，有些地方会放不下

	//坐标和蛇的每个节点坐标比较
	pSnakeNode pcur = ps->pSnake;
	while (pcur)
	{
		if (x == pcur->x && y == pcur->y)
		{
			goto again;  //横坐标和纵坐标都相等，说明位置重合了，就重新生成，直到不一样为止
		}
		pcur = pcur->next;
	}

	//创建食物
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreatFood::malloc error");
		return;
	}
	pFood->x = x;  //pFood的横坐标就是上面随机生成的x
	pFood->y = y;  //pFood的纵坐标就是上面随机生成的y
	ps->pFood = pFood;
	SetPos(x, y);
	wprintf(L"%lc", FOOD);
}

void GameStart(pSnake ps)
{
	//设置控制台的信息:窗口大小，窗口名
	system("mode con cols=100 lines=30");
	system("title ls的贪吃蛇");
	//隐藏光标
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(handle, &CursorInfo);
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态

	//打印欢迎信息
	WelcomeToGame();
	//绘制地图
	CreateMap();
	//初始化蛇
	InitSnake(ps);
	//创建食物
	CreateFood(ps);
}

void PrintHelpInfo()
{
	SetPos(62, 15);
	printf("1.不能穿墙，不能咬到自己");
	SetPos(62, 16);
	printf("2.用↑↓←→来控制蛇的移动");
	SetPos(62, 17);
	printf("3.觉得不够难？试试按一下W吧！");
}

void pause()
{
	while (1)
	{
		Sleep(100);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

int NextIsFood(pSnake ps, pSnakeNode pNext)
{
	if (ps->pFood->x == pNext->x && ps->pFood->y == pNext->y)
		return 1;  //下一个坐标是食物
	else
		return 0;  //下一个坐标不是食物
}

void EatFood(pSnake ps, pSnakeNode pNext)
{
	pNext->next = ps->pSnake;  //让新节点（食物）的下一个节点指向原来的头
	ps->pSnake = pNext;  //让新节点（食物）成为头

	//打印蛇
	pSnakeNode pcur = ps->pSnake;
	while (pcur)
	{
		SetPos(pcur->x, pcur->y);
		wprintf(L"%lc", BODY);
		pcur = pcur->next;
	}
	ps->Score += ps->FoodWeight;
	//释放旧的食物
	free(ps->pFood);
	//新建新的食物
	CreateFood(ps);
}

void NotEatFood(pSnake ps, pSnakeNode pNext)
{
	pNext->next = ps->pSnake;
	ps->pSnake = pNext;
	pSnakeNode pcur = ps->pSnake;
	while (pcur->next->next)
	{
		SetPos(pcur->x, pcur->y);
		wprintf(L"%lc", BODY);
		pcur = pcur->next;
	}
	SetPos(pcur->next->x, pcur->next->y);
	printf("  ");
	free(pcur->next);
	pcur->next = NULL;
}

void KillByWall(pSnake ps)
{
	if (ps->pSnake->x == 56 || ps->pSnake->x == 0 || ps->pSnake->y == 0 || ps->pSnake->y == 26)
	{
		ps->status = KILL_BY_WALL;
	}
}

void KillBySelf(pSnake ps)
{
	pSnakeNode pcur = ps->pSnake->next->next;  //从第三个节点开始，因为前两个节点不可能撞得到
	while (pcur)
	{
		if (pcur->x == ps->pSnake->x && pcur->y == ps->pSnake->y)
		{
			ps->status = KILL_BY_SELF;
			return;
		}
		pcur = pcur->next;
	}
}

void SnakeMove(pSnake ps)
{
	//创建一个新节点，表示蛇移动，如果吃了食物，加一个蛇身，没加就移动
	pSnakeNode pNext = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNext == NULL)
	{
		perror("SnakeMove::malloc");
		return;
	}
	pNext->next = NULL;

	switch (ps->direction)
	{
	case UP:
		pNext->x = ps->pSnake->x;
		pNext->y = ps->pSnake->y - 1;
		break;
	case DOWN:
		pNext->x = ps->pSnake->x;
		pNext->y = ps->pSnake->y + 1;
		break;
	case LEFT:
		pNext->x = ps->pSnake->x - 2;
		pNext->y = ps->pSnake->y;
		break;
	case RIGHT:
		pNext->x = ps->pSnake->x + 2;
		pNext->y = ps->pSnake->y;
		break;
	}

	//判断下一个坐标是否是食物
	if (NextIsFood(ps, pNext))
	{
		//是食物就吃掉
		EatFood(ps, pNext);
	}
	else
	{
		//不是食物就继续走
		NotEatFood(ps, pNext);
	}

	//检测撞墙
	KillByWall(ps);
	//检测撞到自己
	KillBySelf(ps);
}

void GameRun(pSnake ps)
{
	//打印帮助信息
	PrintHelpInfo();
	do 
	{
		//打印当前的分数
		SetPos(62, 10);
		printf("总分：%5d\n", ps->Score);
		SetPos(62, 11);
		printf("食物分数：%02d\n", ps->FoodWeight);
		//监测按键
		//上，下，左，右，ESC，空格，w
		if (KEY_PRESS(VK_UP) && ps->direction != DOWN)
		{
			ps->direction = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->direction != UP)
		{
			ps->direction = DOWN;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->direction != LEFT)
		{
			ps->direction = RIGHT;
		}
		else if (KEY_PRESS(VK_LEFT))
		{
			ps->direction = LEFT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->status = ESC;
			break;
		}
		else if (KEY_PRESS(0x57))
		{
			ps->Speed = 120;
		}
		//睡眠一下
		Sleep(ps->Speed);
	    //走一步
		SnakeMove(ps);
	} while (ps->status == OK);
}

void GameEnd(pSnake ps)
{
	SetPos(15, 12);
	switch (ps->status)
	{
	case ESC:
		printf("您已正常退出！\n");
		break;
	case KILL_BY_WALL:
		printf("很遗憾，您撞墙了，游戏结束！\n");
		break;
	case KILL_BY_SELF:
		printf("很遗憾，您咬到自己了，游戏结束！\n");
		break;
	}

	//释放贪吃蛇的链表资源
	pSnakeNode pcur = ps->pSnake;
	pSnakeNode del = NULL;
	while (pcur)
	{
		del = pcur;
		pcur = pcur->next;
		free(del);
	}
	ps->pSnake = NULL;
	free(ps->pFood);
}